<?php

/**
 * TVB LOGIN MODULE
 * 
 * class TVBCrypt
 * this is authentication class.
 * @package TVB
 * @author  William.Hu <william.hu@achievo.com>
 * @version $Id: Crypt.php,v0.1 2008/06/20 $;
 */
class TVBCrypt
{

    /**
     * Constructor.
     *
     * @param string $path file directory.
     * @access public
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * openssl encrypt
     * @param string $source
     * @param string $type
     * @return string
     */
    public static function encrypt($source, $key)
    {
	if(!$source||!$key){return false;}
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
        $encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, trim($source), MCRYPT_MODE_ECB, $iv);
        return trim(base64_encode($encrypted_string));
    }

    /**
     * openssl decrypt
     * @param string $source
     * @param string $type
     * @return string
     */
    public static function decrypt($source, $key)
    {
	if(!$source||!$key){return false;}
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
        $decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($source), MCRYPT_MODE_ECB, $iv);
        return trim($decrypted_string);
    }
}
?>
